;(batch cenarios.clp)

;inits the game
(defrule init
    (initial-fact)
    =>
    (assert(stepCounter 1))
    (assert(collectedNenufars 0))
    (assert(chosenScenario facil))
    (assert(frog (linha -8) (coluna -4) ))
    )


(defrule makeStep
    (chosenScenario ?cenario)
    (cenario (nome-cenario ?cenario) (tempo-lim ?maxSteps))
    (stepCounter ?step &: (<= ?step ?maxSteps))
    =>
    (assert(draw))
    )

;draws the board in current state
(defrule afterDrawing
    ?f1 <- (afterDrawing)
    =>
    (retract ?f1)
    (assert(verifyFrogPosition))
    )

;checks if frog was ran over by car
(defrule verifyFrogRanOver
    ?f1 <- (verifyFrogPosition)
    ?f <- (frog (linha ?line) (coluna ?col) (killed FALSE))
    (linha (index ?line) (tipo estrada))
    (mobil (linha ?line) (coluna ?col))
    =>
    (printout t "Killed by car, sorry!" crlf)
    (modify ?f (killed TRUE))
    (retract ?f1)
    )

;checks if frog hasn't sank 
(defrule verifyFrogSank
    ?f1 <- (verifyFrogPosition)
    ?f <- (frog (linha ?line) (coluna ?col) (killed FALSE))
    (linha (index ?line) (tipo rio))
    (not(mobil (linha ?line) (coluna ?col)))
    (not(nenufar (linha ?line) (coluna ?col)))
    =>
    (printout t "Frog sank, sorry!" crlf)
    (modify ?f (killed TRUE))
    (retract ?f1)
    ;(assert(moveObjects))
    )

;checks if frog reached goal already
(defrule frogReachedGoal
    ?f1 <- (verifyFrogPosition)
    ?f <- (frog (linha ?line) (coluna ?col) (killed FALSE))
    ?n <- (nenufar (linha ?line) (coluna ?col))
    ?c <- (collectedNenufars ?collected)
    =>
    (printout t "Congratulation! Parabens!" crlf)
    (retract ?f1)
    (retract ?n)
    (retract ?c)
    (modify ?f (killed TRUE))
    (assert(frog (linha -9) (coluna -4)))
    (assert(moveObjects))
    (assert(collectedNenufars (+ ?collected 1)))
    )

;should be fired if frog is still alive (wasn't killed by car and hasn't sank)
(defrule verifyFrogOK
    (declare (salience -1))
    ?f1 <- (verifyFrogPosition)
    =>
    (retract ?f1)
    ;(printout t "position ok" crlf)
    (assert(moveObjects))
    )

;moves frog to next position
(defrule afterMovingObjects
    ?f1 <- (afterMovingObjects)
    =>
    (retract ?f1)
    
    (assert (moveFrog))
    )

(defrule afterFrogMove
    ?f1 <- (afterFrogMove) ;this fact must be asserted after frog's movemnet in ra_e_ambiente
    ?f2 <- (stepCounter ?step)
    =>
    (retract ?f1)
    (retract ?f2)
    (assert (stepCounter (+ ?step 1)))
    )

;checks if time limit wasn't exceeded
(defrule timeExceeded
    (chosenScenario ?cenario)
    (cenario (nome-cenario ?cenario) (tempo-lim ?maxSteps))
    ?f <- (stepCounter ?step &: (> ?step ?maxSteps))
    (collectedNenufars ?collected)
    =>
    (printout t "Time exceeded" crlf)
    (printout t "collected " ?collected " nenufars" crlf)
    )








